package com.pangda.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.constant.NoticeConstant;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.common.utils.UserContext;
import com.pangda.pojo.*;
import com.pangda.pojo.dto.ContractDto;
import com.pangda.pojo.dto.ContractPageParam;
import com.pangda.pojo.vo.BusinessVo;
import com.pangda.pojo.vo.ContractVo;
import com.pangda.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 合同表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-20
 */
@Api(tags = "合同管理")
@RestController
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    private IContractService service;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private INoticesService noticesService;

     @ApiOperation("合同分页查询")
    @GetMapping("/list")
    public PageResult list(ContractPageParam pageParam) {
        LambdaQueryWrapper<Contract> wrapper = new LambdaQueryWrapper<>();
        Map<String, Object> params = pageParam.getParams();
        if (ObjectUtil.isNotEmpty(params.get("beginCreateTime")) && ObjectUtil.isNotEmpty(params.get("endCreateTime"))) {
            wrapper.between(Contract::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        }
        //合同号
        wrapper.like(StringUtils.hasText(pageParam.getContractNo()), Contract::getContractNo, pageParam.getContractNo());
        wrapper.like(StringUtils.hasText(pageParam.getName()), Contract::getName, pageParam.getName());
        wrapper.eq(StringUtils.hasText(pageParam.getPhone()), Contract::getPhone, pageParam.getPhone());
        wrapper.eq(StringUtils.hasText(pageParam.getSubject()), Contract::getSubject, pageParam.getSubject());
        wrapper.eq(ObjectUtil.isNotNull(pageParam.getCourseId()), Contract::getCourseId, pageParam.getCourseId());
        Page<Contract> page = service.page(new Page<>(pageParam.getPageNum(), pageParam.getPageSize()), wrapper);
        return PageResult.success(page, Contract.class);
    }

    @ApiOperation("合同新增")
    @PostMapping
    public ResponseResult save(@RequestBody ContractDto dto) {
        Contract contract = BeanUtil.copyProperties(dto, Contract.class);
        if (ObjectUtil.isNotEmpty(UserContext.getUser())){
            //从redis中取出文件地址
            String path = redisTemplate.opsForValue().get(UserContext.getUser())+"";
            contract.setFileName(path);
        }
        service.insert(contract);
        Notices notices = new Notices();
        notices.setCreateBy(UserContext.getUser());
        notices.setNoticeType(NoticeConstant.NOTICETYPE_NOTICE);
        notices.setStatus(NoticeConstant.NOTICE_NEW);
        notices.setNoticeContent(contract.getContractNo());
        noticesService.save(new NoticesController().add(notices));
        return ResponseResult.success();
    }

    @ApiOperation("合同详细查询")
    @GetMapping("/detail/{id}")
    public ResponseResult getdetailById(@PathVariable("id") Integer id) {
        Contract byId = service.getById(id);
        return ResponseResult.success(BeanUtil.copyProperties(byId, ContractVo.class));
    }
    @ApiOperation("根据id查询合同")
    @GetMapping("/{id}")
    public ResponseResult getById(@PathVariable("id") Integer id) {
        return ResponseResult.success(service.getById(id));
    }


    @ApiOperation("合同修改")
    @PutMapping
    public ResponseResult  updateById(@RequestBody ContractDto dto) {
        Contract contract = BeanUtil.copyProperties(dto, Contract.class);
        service.updateById(contract);
        return ResponseResult.success();
    }


    @ApiOperation("商机转合同")
    @PutMapping("/changeContract/{id}")
    public ResponseResult changeContract(@PathVariable("id")Integer id,@RequestBody ContractDto dto) {
        Contract contract = BeanUtil.copyProperties(dto, Contract.class);
        Business one = businessService.getOne(new LambdaQueryWrapper<Business>().eq(Business::getId,id));
        Business business = one.setStatus("7");
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        BeanUtil.copyProperties(one,contract);
        if (ObjectUtil.isNotEmpty(UserContext.getUser())){
            String path = redisTemplate.opsForValue().get(UserContext.getUser())+"";
            contract.setFileName(path);
        }
        service.save(contract);
        return ResponseResult.success();
    }
}








